package com.neusoft.bizcore.web.dto.result;

import java.io.Serializable;

import org.springframework.data.domain.Page;

import io.swagger.annotations.ApiModelProperty;

/**
 * page detail info constructed from org.springframework.data.domain.Page
 *
 * @author sunchf
 * @date 2018年12月10日 上午9:40:26
 */
public class PageDetail implements Serializable {

    private static final long serialVersionUID = 8286904591729098621L;

    /**
     * total elements in data source
     */
    @ApiModelProperty(value = "total elements", position = 1)
    private long totalElements;

    /**
     * number of elements this page
     */
    @ApiModelProperty(value = "number of elements", position = 2)
    private int numberOfElements;

    /**
     * total pages in data source
     */
    @ApiModelProperty(value = "total pages", position = 3)
    private int totalPages;

    /**
     * current page number from 0
     */
    @ApiModelProperty(value = "current page number from 0", position = 4)
    private int number;

    /**
     * is first page
     */
    @ApiModelProperty(value = "is first page", position = 5)
    private boolean first;

    /**
     * is last page
     */
    @ApiModelProperty(value = "is last page", position = 6)
    private boolean last;

    /**
     * page size
     */
    @ApiModelProperty(value = "page size", position = 7)
    private int size;

    public static PageDetail convert(final Page<?> page) {
        final PageDetail pageData = new PageDetail();
        pageData.setFirst(page.isFirst());
        pageData.setLast(page.isLast());
        pageData.setNumber(page.getNumber());
        pageData.setTotalPages(page.getTotalPages());
        pageData.setNumberOfElements(page.getNumberOfElements());
        pageData.setSize(page.getSize());
        pageData.setTotalElements(page.getTotalElements());
        return pageData;
    }

    public long getTotalElements() {
        return this.totalElements;
    }

    public void setTotalElements(final long total) {
        this.totalElements = total;
    }

    public int getNumberOfElements() {
        return this.numberOfElements;
    }

    public void setNumberOfElements(final int rows) {
        this.numberOfElements = rows;
    }

    public int getTotalPages() {
        return this.totalPages;
    }

    public void setTotalPages(final int pages) {
        this.totalPages = pages;
    }

    public boolean isFirst() {
        return this.first;
    }

    public void setFirst(final boolean first) {
        this.first = first;
    }

    public boolean isLast() {
        return this.last;
    }

    public void setLast(final boolean last) {
        this.last = last;
    }

    public int getSize() {
        return this.size;
    }

    public void setSize(final int size) {
        this.size = size;
    }

    public int getNumber() {
        return this.number;
    }

    public void setNumber(final int page) {
        this.number = page;
    }

    @ApiModelProperty(value = "the start range numbers", position = 8)
    public long getFromNumber() {
        if (this.numberOfElements == 0) {
            return (this.number * this.size);
        } else {
            return (this.number * this.size) + 1;
        }
    }

    @ApiModelProperty(value = "the end range numbers", position = 9)
    public long getToNumber() {
        return (this.number * this.size) + this.numberOfElements;
    }

}
